using UnityEngine;
using System.Collections.Generic;

namespace UndergroundSurvival.Rooms
{
    public class FarmRoom : BaseRoom
    {
        [System.Serializable]
        public class CropData
        {
            public string cropName;
            public float growthTime;
            public int yield;
            public int waterNeeded;
            public int fertilizerNeeded;
        }

        [Header("Farm Settings")]
        [SerializeField] private int maxCrops = 4;
        [SerializeField] private float waterConsumptionRate = 0.1f;
        [SerializeField] private float fertilizerConsumptionRate = 0.05f;
        [SerializeField] private List<CropData> availableCrops = new List<CropData>();

        private List<CropData> activeCrops = new List<CropData>();
        private List<float> cropGrowthProgress = new List<float>();

        protected override void Update()
        {
            base.Update();

            if (!isBuilt || !isFunctioning) return;

            // 处理农作物生长
            for (int i = activeCrops.Count - 1; i >= 0; i--)
            {
                if (activeCrops[i] != null)
                {
                    // 消耗资源
                    if (ResourceManager.Instance != null)
                    {
                        ResourceManager.Instance.ModifyResource(ResourceType.Water, -waterConsumptionRate * Time.deltaTime);
                        ResourceManager.Instance.ModifyResource(ResourceType.Fertilizer, -fertilizerConsumptionRate * Time.deltaTime);
                    }

                    // 更新生长进度
                    cropGrowthProgress[i] += Time.deltaTime;
                    if (cropGrowthProgress[i] >= activeCrops[i].growthTime)
                    {
                        // 收获农作物
                        HarvestCrop(i);
                    }
                }
            }
        }

        public bool PlantCrop(string cropName)
        {
            if (activeCrops.Count >= maxCrops)
                return false;

            CropData crop = availableCrops.Find(c => c.cropName == cropName);
            if (crop == null)
                return false;

            activeCrops.Add(crop);
            cropGrowthProgress.Add(0f);
            return true;
        }

        private void HarvestCrop(int index)
        {
            if (index < 0 || index >= activeCrops.Count)
                return;

            CropData crop = activeCrops[index];
            if (ResourceManager.Instance != null)
            {
                ResourceManager.Instance.ModifyResource(ResourceType.Food, crop.yield);
            }

            activeCrops.RemoveAt(index);
            cropGrowthProgress.RemoveAt(index);
        }

        public override void OnRoomUpgraded()
        {
            base.OnRoomUpgraded();
            maxCrops += 2;
        }

        public List<CropData> GetAvailableCrops()
        {
            return availableCrops;
        }

        public int GetCurrentCrops()
        {
            return activeCrops.Count;
        }

        public int GetMaxCrops()
        {
            return maxCrops;
        }

        public bool HasSpace()
        {
            return activeCrops.Count < maxCrops;
        }

        public float GetCropProgress(int index)
        {
            if (index < 0 || index >= cropGrowthProgress.Count)
                return 0f;
            return cropGrowthProgress[index] / activeCrops[index].growthTime;
        }
    }
} 